package com.dynamicPlanning;

import com.sun.source.doctree.SeeTree;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class 两个数组的交集 {
    public static void main(String[] args) {
        int[] a = new int[]{1,2,3,2,4,5};
        int[] b = new int[]{2,5,4,2,6};
        int[] intersection = intersection(a, b);
        for (int i : intersection) {
            System.out.println(i);
        }
    }

    public static int[] intersection(int[] a, int[] b){
        if (a == null || a.length == 0 || b == null || b.length == 0){
            return new int[0];
        }
        Set<Integer> setA = new HashSet<>();
        Set<Integer> result = new HashSet<>();
        for (int temp : a) {
            setA.add(temp);
        }
        for (int tempB : b) {
            if(setA.contains(tempB)){
                result.add(tempB);
            }
        }
        int[] res = new int[result.size()];
        int index = 0;
        for (Integer integer : result) {
            res[index++] = integer;
        }
        return res;
    }

}
